package com.servlet;

import com.Impl.collect_productServiceImpl;
import com.Impl.productServiceImpl;
import com.Impl.purchase_recordServiceImpl;
import com.entity.collect_product;
import com.entity.product;
import com.entity.purchase_record;
import com.service.collect_productService;
import com.service.productService;
import com.service.purchase_recordService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;

public class productServlet extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setCharacterEncoding("UTF-8");
    System.out.println("我进来了");
    doGet(req,resp);
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //设置编码
    req.setCharacterEncoding("utf-8");
    resp.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=utf-8");

    //拿执行类型
    String type = req.getParameter("type");
    String page = req.getParameter("page");
    if(type!=null){
      if(page.equals("user")){//用户中心
        switch (type){
          case "load"://用户中心加载
            collect_productService collect_productService = new collect_productServiceImpl();
            List<collect_product> list = collect_productService.findByBidAll(1);
            //查询全部 购买记录
            purchase_recordService purchase_recordService = new purchase_recordServiceImpl();
            List<purchase_record> purchase_record_list = purchase_recordService.findAll();
            int count = purchase_recordService.getCount();
            req.setAttribute("collectList",list);
            req.setAttribute("purchase_record_list",purchase_record_list);
            req.setAttribute("count",count);
            req.getRequestDispatcher("用户中心.jsp").forward(req,resp);
            break;
        }
      }else{
        System.out.println("page参数为空！");
      }
    }else{
      //类型为空时 加载首页
      productService productServlet = new productServiceImpl();
      List<product> list = productServlet.findAll();
      req.setAttribute("array",list);
      req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
  }
}
